package com.bookhouse.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class GetTime {
    public static long getTimeDifference(Timestamp formatTime1,
                                         Timestamp formatTime2) {
        SimpleDateFormat timeformat = new SimpleDateFormat(
                "yyyy-MM-dd,HH:mm:ss");
        long t1 = 0L;
        long t2 = 0L;
        try {
            t1 = timeformat.parse(getTimeStampNumberFormat(formatTime1))
                    .getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            t2 = timeformat.parse(getTimeStampNumberFormat(formatTime2))
                    .getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // ��Ϊt1-t2�õ����Ǻ��뼶,����Ҫ��3600000�ó�Сʱ.����������ͬ��
        long t3 = t1 - t2;
        int hours = (int) ((t1 - t2) / 3600000);
        int minutes = (int) (((t1 - t2) / 1000 - hours * 3600) / 60);
        @SuppressWarnings("unused")
        int second = (int) ((t1 - t2) / 1000 - hours * 3600 - minutes * 60);
        return t3;
    }

    public static String getTimeStampNumberFormat(Timestamp formatTime) {
        SimpleDateFormat m_format = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss",
                new Locale("zh", "cn"));
        return m_format.format(formatTime);
    }

    public static String getTimeStampNumberFormat1(Timestamp formatTime) {
        SimpleDateFormat m_format = new SimpleDateFormat("yyyy年MM月dd日HH时",
                new Locale("zh", "cn"));
        return m_format.format(formatTime);
    }

    public static String getReturnDate(Timestamp formatTime, int day) {
        SimpleDateFormat timeformat = new SimpleDateFormat(
                "yyyy-MM-dd,HH:mm:ss");
        long t1 = 0L;
        try {
            t1 = timeformat.parse(getTimeStampNumberFormat(formatTime))
                    .getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long hours = t1 / 3600000;
        hours = hours + day * 24;
        Timestamp time1 = new Timestamp(hours * 3600000);
        String timeStr = getTimeStampNumberFormat1(time1);
        return timeStr;
    }

    public static long getReturnDateLong(Timestamp formatTime, int day) {
        long t1 = 0L;
        t1 = formatTime.getTime();
        t1 += day * 24 * 3600000;
        return t1;
    }

    public static Timestamp getTimestamp(long a) {
        Timestamp time = new Timestamp(a);
        return time;
    }

    public static long getLong(Timestamp time) {
        SimpleDateFormat timeformat = new SimpleDateFormat(
                "yyyy-MM-dd,HH:mm:ss");
        long t1 = 0L;
        try {
            t1 = timeformat.parse(getTimeStampNumberFormat(time)).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return t1;
    }
}
